# -*- coding: utf-8 -*-

import forward_client
import forward_login
import logic.network.client_rpc as client_rpc
import logic.entitys.entity_utils as entity_utils
import logic.common.callout as callout


# 接收到其他客户端的处理(保存它的login data到本地，根据need_sync来判断是否要把自己的数据同步给它)
def recv_login(src_eid, need_sync, login_data):
    print("Receive other client login, eid = {}".format(src_eid))


def get_login_data():
    return None


def client_network_init():
    print("Client network init")
    forward_client.Set(forward_client.ForwardClient())
    print(forward_client.Get())
    print(forward_client.Get().__module__)
    forward_login.Set(forward_login.ForwardLogin())

    forward_client.Get().setup('127.0.0.1', 55340)
    forward_login.Get().bind_recv_login(recv_login)
    client_rpc.rpc_init()
    callout.callout(1, forward_client.Get().tick, forever=True)
    forward_client.Get().wait_connected(5)
    # 广播登录信息
    forward_login.Get().broadcast_login(entity_utils.get_myself_player_id(), get_login_data())
    # rpc init test
    client_rpc.rpc_bind("RPC_TEST", _rpc_test)
    client_rpc.rpc_call("RPC_TEST", -1, {"A": 100, 9: "aa"}, "ha_ha", c=8786)


def client_network_fini():
    print("Client network fini")
    forward_login.Get().broadcast_logout()
    client_rpc.rpc_fini()
    forward_client.Get().close_conn()
    entity_utils.reset_myself_player_id()

    forward_client.Set()
    forward_login.Set()


def _rpc_test(src_eid, a=1, b=2, c=3):
    print("## Receive RPC_TEST from {} message: {}, {}, {}".format(src_eid, a, b, c))
